<?php

namespace app\bbxw\command;


use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use think\admin\Command;
use think\console\Input;
use think\console\Output;
/**
 * 订单导入
 * Class SmsNum
 * @package app\common\command
 */
class UpdataCustomer extends Command
{
    /**
     * 配置指令
     */
    protected function configure()
    {
        $this->setName('xbbxw:updataCustomer');
        $this->setDescription('UpdataCustomer');
    }

    protected function execute(Input $input, Output $output)
    {
        $this->setQueueProgress("正在解析数据,请稍等...", 0);
        $queue_data = $this->queue->data;
        $num = $queue_data['num'];
        $q = $this->app->db->name("play_accompany")->order('id desc');
        if($num>0){
            $q->limit(0, $num);
        }
        $q2 = $q;
        $ids = $q->column('id');
        $power_sources =$q2->column("power_source");
        foreach ($power_sources as $v){
            $power_source_id = $this->app->db->name("power_source")->where("name",$v)->value('id');
            if(empty($power_source_id)){
                $power_source_id = $this->app->db->name("power_source")->insertGetId([
                    'name'=>$v,
                    'password'=>md5(123123)
                ]);
            }
        }

        $total = count($ids);
        $count = 0;
        $prestore_amounts = $this->app->db->name("play_accompany")->whereIn("id",$ids)
            ->field("sum(prepaid_amount) as prestore_total_amount,customer_id,sum(business_amount) as use_total_amount")
            ->group('customer_id')->select()->toArray();
        foreach ($prestore_amounts as $v){
            $this->app->db->name("customer")->where(['id'=>$v['customer_id']])
                ->inc('use_total_amount',(float)$v['use_total_amount'])->inc('prestore_total_amount',(float)$v['prestore_amount'])->update();
            $this->queue->message($total, ++$count, "正在更新金额...");
        }

        $this->setQueueSuccess("数据更新成功,请刷新页面");
    }
}